/**
 * 
 */
package com.scpii.universal.hibernate.dao.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.hibernate.dao.ContentDao;
import com.scpii.universal.hibernate.entity.ContentTab;

/**
 * genericWeb
 * @author weny
 * 2012-7-6上午10:52:26
 */
@Repository
public class ContentDaoImpl extends BaseDaoImpl<ContentTab, Integer> implements ContentDao {

	@Override
	public void delete(String cateIds) {
		String hql = "update ContentTab c set c.intDeleteSign=?,c.datUpdateDatetime=? "
			+ "where c.intCateId in" + cateIds;
		getHibernateTemplate().getSessionFactory().getCurrentSession()
			.createQuery(hql).setParameter(0,
					GenericConstants.DELETE_SIGN_DELETED).setParameter(
					1,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
							.format(new Date())).executeUpdate();
	}

	@Override
	public int getMaxSortOrder(int intCateId) {
		String hql = "select max(c.intSortOrder) from ContentTab c where c.intDeleteSign=? and c.intCateId=?";
		int maxSortOrder = (Integer) getHibernateTemplate().getSessionFactory()
				.getCurrentSession().createQuery(hql)
				.setParameter(0, GenericConstants.DELETE_SIGN_EXIST)
				.setParameter(1, intCateId).uniqueResult();
		return maxSortOrder;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ContentTab> getByUserCateIds(int userId, int cateId) {
		String hql = "select new ContentTab(c.intContentId,intResourceId,intPrivilegeUser) from ContentTab c " +
				"where c.intDeleteSign=? and c.intPrivilegeUser>0 and c.intCateId=?";
		return (List<ContentTab>) getHibernateTemplate().getSessionFactory()
				.getCurrentSession().createQuery(hql)
				.setParameter(0, GenericConstants.DELETE_SIGN_EXIST)
				.setParameter(1, cateId).list();
	}

	@Override
	public Integer getContentIdByResourceId(Long intResourceId) {
		StringBuffer hql = new StringBuffer("select new ContentTab(c.intContentId,c.intResourceId) from ContentTab c ");
		hql.append("where c.intDeleteSign=:intDeleteSign and c.intResourceId=:intResourceId");
		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("intResourceId", intResourceId);
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		List<ContentTab> contents =  this.hqlGetList(hql.toString(), argsMap);
		return contents.get(0).getIntContentId();
	}

}
